FieldUtil = {
	addField : function() {
		$("#tabbar_panel li").removeClass("active");
		$("#tabbar_panel li:eq(0)").addClass("active");

		var child = App.FieldSelectionView.create();
		var containerView = Em.View.views['nav_tab_view'];
		containerView.get('childViews').forEach(function(item) {
			item.remove();
		});
		containerView.removeAllChildren();
		containerView.get('childViews').pushObject(child);
	},
	editFieldSettings : function(id, code) {
		
		if (id && id != "0") {
			Vari.CurrFieldId = id;
		}
		if (code) {
			Vari.CurrFieldCode = code;
		}		

		if (!Vari.CurrFieldId || !Vari.CurrFieldCode) {
			return false;
		}
		
		$("#tabbar_panel li").removeClass("active");
		$("#tabbar_panel li:eq(1)").addClass("active");
		
		var child = null;
		var settingsViewName = Util.capFirstLetter(Vari.CurrFieldCode) + "SettingsView";
		
		child = eval("App." + settingsViewName + ".create()");

		var containerView = Em.View.views['nav_tab_view'];
		containerView.get('childViews').forEach(function(item) {
			item.remove();
		});
		containerView.removeAllChildren();
		
		containerView.get('childViews').pushObject(child);
		child.set("context", Vari.fieldMap.get(Vari.CurrFieldId));
	},
	removeField: function (id, code){
		var f = Vari.frm.get("fields").filterProperty('uid', id)[0];
		Vari.frm.get("fields").removeObject(f);
		Vari.fieldMap.remove(id);	
		$("#" + id).remove();
	},
	
	genFiledVos: function (fields){
		var fieldVos = [];
		fields.forEach(function(fieldTo) {
			var fieldVo = FieldUtil.genFieldVo(fieldTo);			
			fieldVos.pushObject(fieldVo);
		});
		return fieldVos;
	},
	genFieldVo: function (fieldTo){
		var fieldVo = App.FieldVo.create();
		
		Ember.mixin(fieldVo, {
			uid: fieldTo.uid,
			type: fieldTo.type,
			name: fieldTo.name,
			valid: fieldTo.valid //Ember.Object.create(fieldTo.valid)
		});
		if (fieldTo.type == "sectionBreak") {
			Ember.mixin(fieldVo, {
				desc: fieldTo.desc
			});
		} else if (fieldTo.type == "image") {
			Ember.mixin(fieldVo, {
				src: fieldTo.src
			});
		} else if (fieldTo.type == "phone") {
			Ember.mixin(fieldVo, {
				format: fieldTo.format
			});
		} else if (fieldTo.type == "price") {
			Ember.mixin(fieldVo, {
				currency: fieldTo.currency
			});
		} else if (fieldTo.type == "multipleChoice" || fieldTo.type == "checkboxes" || fieldTo.type == "dropdown") {			
			var optVos = [];
			fieldTo.options.forEach(function(optTo) {						
				var optVo = Ember.Object.create(optTo);
				Ember.mixin(optVo, {
					group: fieldTo.uid
				});
				optVos.pushObject(optVo);
				Vari.fieldMap.set(optVo.get("uid"), optVo);
			});
			Ember.mixin(fieldVo, {
				options: optVos,
			});
			
			if (fieldTo.type == "dropdown"){
				Ember.mixin(fieldVo, {
					isMultiple: fieldTo.isMultiple
				});
			} 
		}
		return fieldVo;
	}
};